package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements Operation{
    @Override
    public void work(BookList book) {
        System.out.println("删除图书");
        System.out.println("请输入要删除书的名称：");
        Scanner sc = new Scanner(System.in);
        String delName = sc.nextLine();
        int currentSize = book.getUsedsize();
        for (int i = 0; i < currentSize; i++) {
            if (delName.equals(book.getBooks(i).getName())) {
                int rounds = currentSize-i-1;
                if (i == currentSize - 1) {//如果要删除的是最后一本书
                    book.setUsedsize(currentSize-1);
                    System.out.println("删除成功！");
                    return;
                } else {
                    Book[] bookArray = book.getBookArray();
                    while (rounds != 0) {
                        bookArray[i] = book.getBooks(i+1);
                        rounds--;
                        i++;
                    }
                    bookArray[i] = null;
                    book.setUsedsize(currentSize-1);
                    System.out.println("删除成功！");
                    return;
                }
            }
            System.out.println("没有这本书！");
        }
    }
}
